﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil.CustomObject;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Numeric
{   
    [Serializable]
    [VerfityAttribute(Id = "VerfityD0401", Name = "行列为特定项目时抽出", CategoryType = EnumCategoryType.数值, Model = true)]
    public class VerfityD0401 : VerfityOneResult
    {
        private CustomCollection<SpecialStringArray> _datalist = new CustomCollection<SpecialStringArray>() { Description = "指定数据列表" };
         

        [Category("指定数据列表"), Description("输入指定数据项目")]
        [TypeConverter(typeof(CustomCollectionConverter<SpecialStringArray>))]
        [RefreshProperties(RefreshProperties.All)]
        public CustomCollection<SpecialStringArray> DataList
        {
            get { return _datalist; }
            //set { _rangelist = value; }
        }

        private bool _combineCell = false;

         [Category("合并所有单元格"), Description("合并所有单元格 ，当作一个项目检查")]
        public bool CombineCell { 
            get { return _combineCell; }
            set { _combineCell = value; }
        }

      

 


        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;
            if (entryCommon.Equals("30"))
            {
                return false;
            }

            if (result == null)
            {
                return false;
            }


            //if (!_combineCell)
            //{

                //也可以按组查询 指定行列为特定值 
            bresult = DataList.Where(t =>! Enum.GetName(typeof(EnumSpecialType), t.SpecialType).Contains("Un"))
                                        /*.Where(t => t.SpecialType != EnumSpecialType.UnInclude && 
                                              t.SpecialType != EnumSpecialType.UnEqual )*/
                                              .AsParallel()
                                  .Select(t => t.Group).Distinct()
                                  .Any(g => DataList.Where(t => t.Group.Equals(g))
                                  .All(o =>
                                           {
                                               if (_combineCell)
                                               {
                                                   var entryText = GetCombineResult(result);
                                                   return o.Check(entryText.ToString());
                                               }
                                               else
                                               {
                                                   return CheckSpecificCharacter(result, GetSpeicficResult, o);
                                               }
                                           }));

                if (!bresult)
                {
                    
                    //也可以按组查询 指定行列为特定值 
                    bresult = DataList.Where(t =>  Enum.GetName(typeof(EnumSpecialType),t.SpecialType).Contains("Un"))
                                                    /*t.SpecialType == EnumSpecialType.UnInclude ||
                                                    t.SpecialType == EnumSpecialType.UnEqual*/
                                                    .AsParallel()
                                      .Select(t => t.Group).Distinct()
                                      .Any(g => DataList.Where(t => t.Group.Equals(g))
                                      .Any(o =>
                                               {
                                                   if (_combineCell)
                                                   {
                                                        var entryText = GetCombineResult(result);
                                                        return o.Check(entryText.ToString());
                                                   }
                                                   else
                                                   {
                                                       return  CheckSpecificCharacter(result, GetSpeicficResult, o);
                                                   }}));
                }

        
            if (bresult)
            {
                System.Console.WriteLine(Id);
                System.Console.WriteLine(base.Memo);
            }

            return bresult;
        }

        private StringBuilder GetCombineResult(List<List<string>> result)
        {
            var entryText = new StringBuilder();

            result.ForEach(row => entryText.Append(string.Join(string.Empty, row)));
            return entryText;
        }

        private   string GetSpeicficResult(string colResult)
        {
            var splitResult = new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat, colResult);
            return splitResult .GetNumeric();  
        }

         

    }
}
